<?php
/* 
 *工具类
 */
class ToolsAction extends Action {

 function _initialize() {
        $this->assign('menuName','tools');
        A('Public')->header();

    }
//将导入的合并订单清理成单个订单,一列,方便财务对账.适用于德生堂订单
  public function clean_order(){

  if(($_FILES['upload']['name'] == '') ){
          $this->display();
          return;
   }
   
   $clean_order = array();
    $inputFileName = A('Import')->getCsv();
    if (($handle = fopen($inputFileName, "r")) !== FALSE) {
        while (($data = fgetcsv($handle,  0, ',', '"', '"')) !== FALSE) {
            if(empty($data['0']) )
                continue;
            $c_data = explode(",",$data['0']) ;
            foreach($c_data as $value){
                array_push($clean_order,$value);
                echo $value."<br>";
            }
        }
     } 
  }

 //整理淘宝成交数据
 public function clean_trade_rate(){
     if(empty($_REQUEST['content'])){
         $this->display();
         return;
     } 
     $t = $_REQUEST['content'];
     $k = explode("\n",$t);
     foreach($k as $index=>$value ){
       if($index % 2 == 0){
           $value = str_replace("颜色分类:","",$value);
           echo $value."    ".$k[$index+1]."<br>";
       }
     
     }
 }

 //清理原表,并把清理的数据移动到档案表中
 //$clean_time 开始清理的时间节点
 //if success,return execute record number;
 public function archive($model="Sale",$clean_time = 0){
     $clean_time = (int)$clean_time;
     //如果清理时间小于一个月,则不清理
     if((time() - $clean_time ) < 30*24*60*60 )
         return false;
     $model = strtolower($model); //全部变小写
     $archive_model = $model."_archive";
     $big_model = ucfirst($model);
     $insert_archive_sql = "insert ignore into ".C('DB_PREFIX').$archive_model." select * from ".C('DB_PREFIX').$model." where create_time < ".$clean_time; 
     $insert_id =  M()->execute($insert_archive_sql);
     if($insert_id>0){
         M($big_model)->where("create_time <".$clean_time)->delete();
         return $insert_id;
     }
     return false;
 }

 //任务调度
 public function cronMission(){
     //TODO 暂适用于数据清理
     $clean_time = time()- 2*30*24*60*60; //统一清理2月前的数据
     $tasks = M('Crontab')->findAll();
     foreach( $tasks as $k => $value ){
         $next_time = (int)$value['next_execute_time']; //一定要转换为数字类型
         $diff = $next_time-time();
         if ($diff <0)
             continue;
         //小于8H之内,则执行,这个时间间隔要与linux crontab 运行间隔保持一致.
         if( $diff < 8*60*60 ){  
             $delay_seconds = $value['delay']* $this->_make_seconds( $value['frequency'] );
             $success_count = $this->archive($value['obj'],$clean_time );
             //写入下次执行时间,并记录日志
             $data['next_execute_time'] = $value['next_execute_time'] + $delay_seconds;
             if($success_count > 0){
                 //下次执行时间 以当前执行完毕的时间 +间隔,防止数据库操作过时
                 $data['next_execute_time'] = time() + $delay_seconds;
                 $data['remark'] = $value['obj']."在 ".toDate(time())." 进行了清理,".$success_count."条2月前数据已经进入archive表";
                 //sale, action 双重操作
                 //if success ,then do
                 if($value['obj'] == "sale")
                         $this->archive('sale_detail',$clean_time );
                 else if($value['obj'] == "action"){
                     //clean history
                     $min_action_id = M('Action')->min('id');                 
                     $insert_archive_sql = "insert ignore into ".C('DB_PREFIX')."history_archive select * from ".C('DB_PREFIX')."history where action_id < ".$min_action_id; 
                     $insert_id =  M()->execute($insert_archive_sql);
                     if($insert_id>0){
                         M('History')->where("action_id <".$min_action_id)->delete();
                     }
                 }
             
             }
             else
                  $data['remark'] = $value['obj']."在 ".toDate(time())." 清理失败";
             M('Crontab')->where('id = '.$value['id'])->save($data);
         }
     } 
 }

 public function test(){
     $clean_time = time()- 2*30*24*60*60; //统一清理2月前的数据
     $this->archive('weblog',$clean_time);
}

//由频率类型转为秒
 private function _make_seconds($frequency = ""){
      switch($frequency){
         case "second": 
             return 1;
         case "minute": 
             return 60;
         case "hour": 
             return 60*60;
         case "day": 
             return 24*60*60;
         case "month": 
             return 24*60*60*30; //按30天计算
         case "year": 
             return 24*60*60*365;
      } 
 }

}
?>
